package com.example.tourstor;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class joinGroupActivity extends AppCompatActivity {

    private ListView join_list;
    private List<Group> joinList = new ArrayList<Group>();
    int UID;

    Thread t;
    String mqrs;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_group);

        //获取上一页面传来的UID
        Intent intent=getIntent();
       // UID=intent.getStringExtra("U_id");
        UID=2;


        join_list = (ListView) findViewById(R.id.join_group_list);   //获得子布局
        joinList.clear();

        getData();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==1){
                    GroupAdapter groupAdapter1 = new GroupAdapter(joinGroupActivity.this,
                            R.layout.group_item, joinList);  //关联数据和子布局
                    join_list.setAdapter(groupAdapter1);   //绑定数据和适配器
                }
            }
        };



        join_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {  //点击每一行的点击事件
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long id) {
                final Group group1=joinList.get(position);         //获取点击的那一行
                AlertDialog.Builder builder=new AlertDialog.Builder(joinGroupActivity.this);
                builder.setTitle("退出"+group1.getGid()+"号团队?");
                builder.setMessage("出发时间："+group1.getChufa()+";   目的地："+group1.getMudidi()+";   预计人均费用："+group1.getFeiyong()+";   联系方式："+group1.getDianhua()+";   人数上限："+group1.getRenshu()+";   目前人数："+group1.getMuqian());
                builder.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                new Thread(new Runnable(){
                                    @Override
                                    public void run() {
                                        try{
                                            java.sql.Connection cn = DBOpenHelper.getConn();
                                            String sql4 = "delete from Gruop_User where G_id =? and U_id =?";
                                            PreparedStatement st4 = cn.prepareStatement(sql4);
                                            st4.setInt(1,group1.getGid());
                                            st4.setInt(2,UID);
                                            st4.executeUpdate();
                                        }catch (SQLException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                                showDialog2();
                            }
                        });
                builder.setNegativeButton("取消", null);
                AlertDialog dialog=builder.create();
                dialog.show();
            }
        });

    }

    private void getData(){
        t=new Thread(new Runnable() {
            @Override
            public void run() {
        try {
            java.sql.Connection cn = DBOpenHelper.getConn();
            String sql = "select * from Gruop_User where U_id=?";
            PreparedStatement st = cn.prepareStatement(sql);
            st.setInt(1,UID);
            ResultSet rs = st.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    int gid = rs.getInt("G_id");
                    String sql2 = "select * from team where G_id=?";
                    PreparedStatement st2 = cn.prepareStatement(sql2);
                    st2.setInt(1,gid);
                    ResultSet rs2 = st2.executeQuery();
                    while (rs2.next()){
                        String time = rs2.getString("setOutTime");
                        String des = rs2.getString("destination");
                        String c = rs2.getString("cost");
                        String t = rs2.getString("tell");
                        String r = rs2.getString("number");
                        String sql3 = "select * from Gruop_User where G_id=?";
                        PreparedStatement st3 = cn.prepareStatement(sql3);
                        st3.setInt(1,gid);
                        ResultSet rs3 = st3.executeQuery();
                        int mq=1;
                        while(rs3.next()){
                            mq++;
                        }
                        String mqrs = String.valueOf(mq);
                        joinList.add(new Group(gid,des,time,c,t,r,mqrs));
                    }
                }
            }else{
                Intent intent = new Intent(joinGroupActivity.this,NoGroupActivity.class);
                startActivity(intent);
            }
            //DBOpenHelper.closeAll(cn, st);
        } catch (SQLException e) {
            e.printStackTrace();
        }
                Message msg = handler.obtainMessage(1);
                msg.what = 1;
                //msg.obj = bundle;
                handler.sendMessage(msg);
            }
        });
        t.start();

    }


    private void showDialog2(){
        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        builder.setTitle("退出成功");
        builder.setMessage("您已成功退出该团队");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Intent intent = new Intent(joinGroupActivity.this,MainActivity.class);
                intent.putExtra("U_id",UID);
                startActivity(intent);
            }
        });
        AlertDialog dialog=builder.create();
        dialog.show();
    }

}
